//
//  ViewController.m
//  UI10_UITableView_plist文件
//
//  Created by Scott on 16/3/22.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "TableViewOfContact.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

// 获取字典中所有的keys
@property (nonatomic, retain) NSArray *arrOfKeys;

// 联系人数据字典
@property (nonatomic, retain) NSDictionary *dic;


@end

@implementation ViewController

- (void)dealloc {
    [_arrOfKeys release];
    [_dic release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self handleData];
    [self createTableView];
}


#pragma mark - 知识点1: plist文件的使用
- (void)handleData {
    
    // 读取plist文件(从磁盘读到内存).
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Contacts" ofType:@"plist"];
    
    self.dic = [NSDictionary dictionaryWithContentsOfFile:path];
    
    // 处理数据, 将key值进行升序排序.
    self.arrOfKeys = [[self.dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
}


#pragma mark - tableView
- (void)createTableView {
    
    TableViewOfContact *tableView = [[TableViewOfContact alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    [self.view addSubview:tableView];
    [tableView release];
    tableView.delegate = self;
    tableView.dataSource = self;
    
}


#pragma mark - tableView DataSource
/** 返回section(分区)数. */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.arrOfKeys.count;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSString *key = [self.arrOfKeys objectAtIndex:section];
    
    NSArray *arr = [self.dic objectForKey:key];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *pool = @"pool";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pool];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool] autorelease];
    }
    
    // cell 赋值
    // 找到key
    NSString *key = [self.arrOfKeys objectAtIndex:indexPath.section];
    
    // 根据Key找到对应的数组
    NSArray *arr = [self.dic objectForKey:key];
    
    // 找到联系人小字典
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [dic objectForKey:@"name"];
    
    cell.detailTextLabel.text = [dic objectForKey:@"phoneNumber"];
    
    return cell;
}

/** 右侧索引栏. */
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    return self.arrOfKeys;
    
}

/** 分区头部标题 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return [self.arrOfKeys objectAtIndex:section];

}

#pragma mark - tableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld, %ld", indexPath.section, indexPath.row);
}





#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
